package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;

import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(TravelComment travelComment) {
       travelComment.setId(null);
       //设置游记评论创建时间
        travelComment.setCreateTime(new Date());
        //通过关联评论对象获取评论当前评论id，来判断是普通评论还是回复评论
        String refId = travelComment.getRefComment().getId();
        if(StringUtils.hasLength(refId)){
            //回复评论
            //获取当前评论的评论对象
            TravelComment ReftravelComment = this.get(refId);
            //将被评论的对象存入当前评论对象中
            travelComment.setRefComment(ReftravelComment);
            //设置评论类型
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);
        }
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).get();
    }

    @Override
    public List<TravelComment> list() {
        return travelCommentRepository.findAll();
    }

    @Override
    public List<TravelComment> findByTravelId(String travelId) {
        return  travelCommentRepository.findByTravelId(travelId);
    }

}
